//
//  YMSamplingViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/23.
//

#import "YMSamplingViewController.h"
#import "YMCheckService.h"
#import "YMAddingSampleViewController.h"

@interface YMSamplingViewController ()

@property (nonatomic, strong) UIScrollView *svMain;
@property (nonatomic, strong) UIView *vBg;

@property (nonatomic, strong) UILabel *lblTips;
@property (nonatomic, strong) UIView *vImage;

@property (nonatomic, strong) UIButton *btnFinish;

@end

@implementation YMSamplingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self loadData];
}

- (void)setUI{
    [self showNavTitle:KLanguage(@"sampling_2_txt")];
    [SNavUtil rightIcon:self icon:@"tools_nav_home_icon" onClick:^{
        [YMBridging goHomeTabWithAlert];
    }];
    WEAK_SELF;
    [SNavUtil leftIcon:self icon:@"tools_nav_left_back" onClick:^{
        [SJumpUtil popVC:weakSelf animated:YES];
    }];
    [self.view addSubview:self.svMain];
    [self.svMain addSubview:self.vBg];
    [self.vBg addSubview:self.lblTips];
    [self.vBg addSubview:self.vImage];
    
    [self.view addSubview:self.btnFinish];
    
    [self.svMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo(0);
    }];
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.equalTo(self.svMain);
        make.bottom.mas_equalTo(0);
    }];
    [self.lblTips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo([SFitUtil fit:15]);
    }];
    [self.vImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lblTips.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo([SFitUtil fit:-17]);
        make.centerX.mas_equalTo(0);
    }];
    [self.btnFinish mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT-[SFitUtil fit:15]);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:54]);
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.svMain.mas_bottom).offset([SFitUtil fit:30]);
    }];
}

- (void)loadData{
    WEAK_SELF;
    [SHUD showWait:self.view];
    [YMCheckService requestCheckSampleStepOneDataWithId:self.idStr stepFirst:YES success:^(NSString * _Nonnull tipsStr, NSArray * _Nonnull arrImg) {
        if (weakSelf == nil) {
            return;
        }
        [SHUD hide:weakSelf.view];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = [SFitUtil fit:8];
        NSAttributedString *attr = [[NSAttributedString alloc] initWithString:tipsStr attributes:@{NSParagraphStyleAttributeName: style}];
        weakSelf.lblTips.attributedText = attr;
        
        if (weakSelf.vImage.subviews > 0){
            [weakSelf.vImage removeAllSubviews];
        }
        UIImageView *imgvPre = nil;
        for (int i = 0; i < arrImg.count; i++) {
            if (weakSelf == nil) {
                return;
            }
            NSDictionary *dictItem = [SDataUtil dictOf:arrImg[i]];
            NSString *imgUrl = [SDataUtil stringOf:dictItem[@"url"] valid:@""];
            __block UIImageView *imgv = [[UIImageView alloc] init];
            
            [weakSelf.vImage addSubview:imgv];
            [imgv mas_makeConstraints:^(MASConstraintMaker *make) {
                if (imgvPre == nil){
                    make.top.mas_equalTo(0);
                }else{
                    make.top.equalTo(imgvPre.mas_bottom).offset([SFitUtil fit:10]);
                }
                make.size.mas_equalTo([SFitUtil fitSize:254 height:397]);
                make.centerX.mas_equalTo(0);
                if (i == arrImg.count-1){
                    make.bottom.mas_equalTo(0);
                }
            }];
            [imgv sd_setImageWithURL:[NSURL URLWithString:[SDataUtil stringOf:imgUrl valid:@""]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                if (error) {
                    return;
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                   CGFloat width = image.size.width;
                   CGFloat height = image.size.height;
                   if (width > 0 && height > 0) {
                       height = ([SFitUtil fit:254]/width)*height;
                       [imgv mas_updateConstraints:^(MASConstraintMaker *make) {
                           make.size.mas_equalTo(CGSizeMake([SFitUtil fit:254], height));
                       }];
                       [imgv layoutIfNeeded]; // 强制重新布局
                   }
               });
            }];
            imgvPre = imgv;
        }
    } failure:^(NSString * _Nonnull message) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:message];
    }];
}


- (void)startClickAction:(id)sender{
    
    YMAddingSampleViewController *vc = [[YMAddingSampleViewController alloc] init];
    vc.idStr = self.idStr;
    WEAK_SELF;
    vc.finishBlock = ^{
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        if (weakSelf.finishBlock){
            weakSelf.finishBlock([dateFormatter stringFromDate:[NSDate date]]);
        }
    };
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
    
}

#pragma mark -- setter and getter
- (UIScrollView *)svMain{
    if (_svMain == nil){
        _svMain = [[UIScrollView alloc] init];
        _svMain.showsVerticalScrollIndicator = YES;
        _svMain.showsHorizontalScrollIndicator = NO;
        _svMain.clipsToBounds = YES;
        _svMain.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _svMain;
}
- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
        _vBg.clipsToBounds = YES;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _vBg;
}
- (UILabel *)lblTips{
    if (_lblTips == nil){
        _lblTips = [SThemeUtil boldLabel:@"" color:STheme.H_D1 fontSize:16];
        _lblTips.numberOfLines = 0;
    }
    return _lblTips;
}
- (UIView *)vImage{
    if (_vImage == nil){
        _vImage = [[UIView alloc] init];
    }
    return _vImage;
}
- (UIButton *)btnFinish{
    if (_btnFinish == nil){
        _btnFinish = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnFinish setTitle:KLanguage(@"add_smapling_done_txt") forState:UIControlStateNormal];
        [_btnFinish setTitleColor:STheme.D9 forState:UIControlStateNormal];
        _btnFinish.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnFinish addTarget:self action:@selector(startClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnFinish.backgroundColor = STheme.D12;
        _btnFinish.layer.cornerRadius = [SFitUtil fit:10];
        _btnFinish.clipsToBounds = YES;
    }
    return _btnFinish;
}


@end
